package com.gali.shop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.gali.shop.mapper.TbBrandMapper;
import com.gali.shop.pojo.TbBrand;
import com.gali.shop.pojo.TbBrandExample;
import com.gali.shop.service.TbBrandService;
import com.gali.shop.utils.PageResult;
import com.gali.shop.utils.QueryBeanPage;
import com.gali.shop.utils.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @author gali
 * @description:
 * @date 2022/6/28 10:54
 */
@Service
public class TbBrandServiceImpl implements TbBrandService {

    @Autowired
    private TbBrandMapper tbBrandMapper;

    public ResultUtil findOne(Long id) {
        TbBrand tbBrand = tbBrandMapper.selectByPrimaryKey(id);
        if (tbBrand!=null){
            return new ResultUtil(true,"success",tbBrand);
        }else{
            return new ResultUtil(false,"error");
        }
    }

    public PageResult findAll(QueryBeanPage queryBeanPage) {
        PageHelper.startPage(queryBeanPage.getPageNum(),queryBeanPage.getPageSize());
        Page<TbBrand> page= (Page<TbBrand>) tbBrandMapper.selectByExample(null);
        return new PageResult(page.getTotal(),page.getResult());
    }

    public ResultUtil addBrand(TbBrand tbBrand) {
        boolean flag=tbBrandMapper.insertSelective(tbBrand)>0;
        if (flag){
            return new ResultUtil(true,"success");
        }else{
            return new ResultUtil(false,"error");
        }
    }

    public ResultUtil removeBrand(Long []ids) {
        boolean flag= tbBrandMapper.deleteBatchByIds(ids)>0;
        if (flag){
            return new ResultUtil(true,"success");
        }else {

        } return new ResultUtil(false,"error");
    }

    public ResultUtil reviseBrand(TbBrand tbBrand) {
        boolean flag=tbBrandMapper.updateByPrimaryKeySelective(tbBrand)>0;
        if (flag){
            return new ResultUtil(true,"success");
        }else{
            return new ResultUtil(false,"error");
        }
    }

    public PageResult selectKeyWord(QueryBeanPage queryBeanPage,TbBrand tbBrand) {
        PageHelper.startPage(queryBeanPage.getPageNum(),queryBeanPage.getPageSize());
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria=example.createCriteria();
        if (tbBrand!=null){
            if (tbBrand.getName()!=null&&tbBrand.getName().length()>0){
                criteria.andNameLike("%"+tbBrand.getName()+"%");
            }
            if (tbBrand.getFirstChar()!=null&&tbBrand.getFirstChar().length()>0){
                criteria.andFirstCharEqualTo(tbBrand.getFirstChar());
            }
        }
        Page<TbBrand> page = (Page<TbBrand>) tbBrandMapper.selectByExample(example);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Map> selectBrand() {
        List<Map> maps = tbBrandMapper.selectBrand();
        return maps;
    }
}
